package com.kancy.vip.pattern.adapter.v2.impl;

import com.kancy.vip.pattern.adapter.v2.LoginAdapter;
import com.kancy.vip.pattern.adapter.v2.LoginForThirdService;
import com.kancy.vip.pattern.adapter.v2.Result;
import com.kancy.vip.pattern.adapter.v2.User;
import com.kancy.vip.pattern.adapter.v2.adapters.QQLoginAdapter;

/**
 * @author kancy
 * @date 2019/3/17 16:54
 */
public class LoginForThirdServiceImpl extends LoginServiceImpl implements LoginForThirdService {

	private Result processLogin(Class<? extends LoginAdapter> loginAdapterClazz, User user){
		assert loginAdapterClazz!=null:"loginAdapterClazz is null";
		try {
			LoginAdapter adapter = loginAdapterClazz.newInstance();
			if(adapter.support(adapter)){
				return adapter.login(user, adapter);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Result result = new Result();
		result.setCode("9999");
		result.setMsg("登录失败");
		return result;
	}

	@Override
	public Result loginForQQ(User user) {
		return processLogin(QQLoginAdapter.class, user);
	}

	@Override
	public Result loginForWecha(User user) {
		return processLogin(QQLoginAdapter.class, user);
	}

	@Override
	public Result loginForSina(User user) {
		return processLogin(QQLoginAdapter.class, user);
	}

	@Override
	public Result loginForMobile(User user) {
		return processLogin(QQLoginAdapter.class, user);
	}
}
